<?php
/**
 * 财务统计
 * User: admin
 * Date: 2017/8/2
 * Time: 19:26
 */

namespace Admin\Controller;


class FinancialController extends AdminController
{

    /**
     * 财务对账
     */
    public function index(){
        $p=I('p');
        if($p){
            $p=$p-1;
            $p=$p*10;
        }else{
            $p=0;
        }
        $param=I('');
        $begin_time=strtotime(I('begin_time'));
        if(I('end_time')) {
            $end_time = strtotime('+1 day', strtotime(I('end_time')));
        }
        if(I('school')){
            $sch['school']=array('like',$param['school']);
            $id=M('school')->where($sch)->getField('id');
            $users=M('member')->where('school_id='.$id)->select();

            $uids=array();
            foreach ($users as &$u){
                $uids[]=$u['uid'];
            }
            $where['uid'] =array('in',$uids);
        }
        $where['title'] =array('like','%'.$param['title'].'%');
        $shops = M('shop')->where($where)->order('id desc')->limit($p,10)->select();
        $total_order=0;
        $total_ying =0;
        $total_money=0;
        $total_cash=0;
        $total_tui=0;
        foreach ($shops as &$sp){
            //已支付总订单数
            $map['shop_id'] =$sp['id'];
            $map['pay_status'] =array('in',array('1','2'));
            if($begin_time||$end_time) {
                $map['create_time'] = array('between', array($begin_time, $end_time));
            }
            $order_count=M('order')->where($map)->count();
           // echo M('order')->getLastSql();exit;
            if($order_count) {
                $sp['order_count'] = $order_count;
                $total_order +=$order_count;
            }else{
                $sp['order_count'] = 0;
            }
            //商家余额
            $shop_money=M('user','eat_')->where('uid='.$sp['uid'])->getField('money');
            if($shop_money){
                $sp['shop_money'] = $shop_money;
                $total_money += $shop_money;
            }else{
                $sp['shop_money'] =0;
            }
            //商家提现总额
            $cmap['type'] =2;
            $cmap['uid'] = $sp['uid'];
            if($begin_time||$end_time) {
                $cmap['createtime'] = array('between', array($begin_time, $end_time));
            }
            $cash=M('user_trade','eat_')->where($cmap)->field('SUM(money) as money')->find();
            $shop_cash=$cash['money'];
            if($shop_cash){
                $sp['shop_cash'] =$shop_cash;
                $total_cash +=$shop_cash;
            }else{
                $sp['shop_cash'] =0;
            }
            //营业额显示
            $ymap['shop_id'] =$sp['id'];
            $ymap['pay_status'] =1;
            if($begin_time||$end_time) {
                $ymap['create_time'] = array('between', array($begin_time, $end_time));
            }
            $ying =M('order')->where($ymap)->field('SUM(price) as price')->find();

            $shop_ying=$ying['price'];
            if($shop_ying) {
                $sp['shop_ying'] = $shop_ying;
                $total_ying +=$shop_ying;
            }else{
                $sp['shop_ying'] = 0;
            }
            //退款额 pay_status=2
            $tmap['shop_id']=$sp['id'];
            $tmap['pay_status']=2;
            if($begin_time||$end_time) {
                $tmap['create_time'] = array('between', array($begin_time, $end_time));
            }
            $tui =M('order')->where($tmap)->field('SUM(price) as price')->find();
            if($tui['price']) {
                $sp['tuikuang'] = $tui['price'];
                $total_tui +=$tui['price'];
            }else{
                $sp['tuikuang'] = 0;
            }
        }

        //统计全部店铺的
        $total_shops = M('shop')->where($where)->order('id desc')->select();
        $total_order=0;
        $total_ying =0;
        $total_money=0;
        $total_cash=0;
        $total_tui=0;
        foreach ($total_shops as &$sp){
            //已支付总订单数
            $map['shop_id'] =$sp['id'];
            $map['pay_status'] =1;
            if($begin_time||$end_time) {
                $map['create_time'] = array('between', array($begin_time, $end_time));
            }
            $order_count=M('order')->where($map)->count();
            // echo M('order')->getLastSql();exit;
            if($order_count) {
                $total_order +=$order_count;
            }
            //商家余额
            $shop_money=M('user','eat_')->where('uid='.$sp['uid'])->getField('money');
            if($shop_money){
                $total_money += $shop_money;
            }
            //商家提现总额
            $cmap['type'] =2;
            $cmap['uid'] = $sp['uid'];
            if($begin_time||$end_time) {
                $cmap['createtime'] = array('between', array($begin_time, $end_time));
            }
            $cash=M('user_trade','eat_')->where($cmap)->field('SUM(money) as money')->find();
            $shop_cash=$cash['money'];
            if($shop_cash){
                $total_cash +=$shop_cash;
            }
            //营业额显示
            $ymap['shop_id'] =$sp['id'];
            $ymap['pay_status'] =1;
            if($begin_time||$end_time) {
                $ymap['create_time'] = array('between', array($begin_time, $end_time));
            }
            $ying =M('order')->where($ymap)->field('SUM(price) as price')->find();

            $shop_ying=$ying['price'];
            if($shop_ying) {
                $total_ying +=$shop_ying;
            }
            //退款额 pay_status=2
            $tmap['shop_id']=$sp['id'];
            $tmap['pay_status']=2;
            if($begin_time||$end_time) {
                $tmap['create_time'] = array('between', array($begin_time, $end_time));
            }
            $tui =M('order')->where($tmap)->field('SUM(price) as price')->find();
            if($tui['price']) {
                $total_tui +=$tui['price'];
            }
        }

        $num=M('shop')->where($where)->order('id asc')->count();
        $this->setPage($num);
        $this->assign('shops',$shops);
        $this->assign('param',$param);
        $this->assign('total_order',$total_order);
        $this->assign('total_money',$total_money);
        $this->assign('total_cash',$total_cash);
        $this->assign('total_ying',$total_ying);
        $this->assign('total_tui',$total_tui);
        $this->display();
    }


    /**
     * 申请提现
     */
    public  function  cashList(){
        $username=I('username');
        if($username){
            $uid=M('ucenter_member')->where('username='.$username)->getField('id');
            $map['uid']=$uid;
        }
        $map['type']=2;
        $map['paytype'] = 1;

        $list=$this->lists('user_trade', $map);
        foreach ($list as &$li){
            $li['phone'] = M('ucenter_member')->where('id='.$li['uid'])->getField('username');
            $user = M('member')->where('uid='.$li['uid'])->find();
            if($li['mode'] == 1){
                $li['mode_txt'] ="支付宝";
                $li['pay_account'] = $user['alipay'];
                $li['username'] = $user['realname'];

            }else if($li['mode'] == 2){
                $li['mode_txt'] ="微信";
                $li['pay_account'] = $user['wechat'];
                $li['username'] = $user['wechat_name'];

            }
        }

        $this->assign('_list', $list);
        $this->meta_title = "申请提现列表";
        $this->display();
    }

    /**
     * 确认提现
     */
    public function confirmcash(){
        $id = I('id');
        $info =M('user_trade')->where('id='.$id)->find();
        if($info){
            $map['paytype'] = 2;
            $map['uptime'] = time();
            $res = M('user_trade')->where('id='.$id)->save($map);
            if($res){
                $this->success('确认成功！');
            } else {
                $this->error('确认失败！');
            }
        }
    }

    /**
     * 消费记录
     */
    public function  records(){
        $username=I('username');
        $map='';
        if($username){
            $uid=M('ucenter_member')->where('username='.$username)->getField('id');
            $map['uid']=$uid;
        }
        //$list = M('user_trade')->where($map)->order('createtime desc')->select();
        $list=$this->lists('user_trade', $map,'createtime desc');
        foreach ($list as &$li){
            $li['username'] = M('ucenter_member')->where('id='.$li['uid'])->getField('username');
            if($li['type'] == 1){
                $li['type_txt'] = "充值";
            }else if($li['type'] == 2){
                if($li['paytype'] ==1) {
                    $li['type_txt'] = "申请提现";
                }else if ($li['paytype'] == 2){
                    $li['type_txt'] = "提现成功";
                }
            }else if($li['type'] == 3){
                $li['type_txt'] = "购买商品";
            }else if($li['type'] == 4){
                $li['type_txt'] = "卖出商品";
            }else if($li['type'] == 5){
                $li['type_txt'] = "分销佣金提成";
            }else if($li['type'] == 6){
                $li['type_txt'] = "退款";
            }else if($li['type'] == 9){
                $li['type_txt'] = "邀请送播豆";
            }
            if($li['pur_type'] ==2){
                $li['money'] =$li['money'] ."积分";
            }else{
                $li['money']  = $li['money']."元";
            }
        }
        $this->assign('_list', $list);
        $this->meta_title = "消费记录列表";
        $this->display();
    }

}